
//
//  MainTabbar.swift
//  SinaWeibo
//
//  Created by 梁楠 on 16/1/11.
//  Copyright © 2016年 QQ主流框架. All rights reserved.
//

import UIKit

class MainTabbar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(plasBtn)
        
    }

    //重写init(frame), 系统默认当前类的对象只能通过代码的方式创建
    //通过xib创建对象,就会奔溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = self.bounds.width/5
        let h = self.bounds.height
        
        let rect = CGRectMake(0, 0, w, h)
        
        var index: CGFloat = 0
        
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRectOffset(rect, w * index, 0)
                
//                if index == 1 {
//                    index++
//                }
//                index++
                index += (index == 1 ? 2 : 1)
            }
        }
        
        plasBtn.frame = CGRectOffset(rect, 2 * w, 0)
        
        //将控件前置
//        bringSubviewToFront(plasBtn)
    }
    
    lazy var plasBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        btn.sizeToFit()
        
        return btn
    }()
    
    
    
    
    
    
    
    
    
    
    
    
    
}
